package com.obao.user.dao.impl;

import com.obao.business.entity.Product;
import com.obao.user.dao.ICollectDao;
import com.obao.user.entity.Collect;
import com.obao.user.entity.User;
import org.hibernate.SessionFactory;

import java.util.List;

/**
 * author 代江平
 * create Date 2016/11/28
 */
public class CollectDao implements ICollectDao {
    private SessionFactory sessionFactory;

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    @Override
    public void save(Collect collect) {
        sessionFactory.getCurrentSession().saveOrUpdate(collect);
    }

    @Override
    public List<Collect> findUserAllCollect(User user) {
        return sessionFactory.getCurrentSession().createQuery("from Collect c where c.user.userId=?").setParameter(0, user.getUserId()).list();
    }

    @Override
    public List<Collect> isExist(Product product, User user) {
       return  sessionFactory.getCurrentSession().createQuery("from Collect c where c.user.userId=? and c.product.productId=?")
                .setParameter(0, user.getUserId())
                .setParameter(1, product.getProductId())
                .list();
    }

    @Override
    public void delete(Integer id) {
        sessionFactory.getCurrentSession().createSQLQuery("DELETE FROM t_collect WHERE collect_id =?").setParameter(0,id).executeUpdate();
    }


}
